// 引入mock
const Mock = require("mockjs");
const Randon = Mock.Random;
let userList = [
  {
    mobile: "123123123123",
    username: "admin",
    password: "123123"
  }
];
let tagList = [
  {
    title: "vue.js",
    content: "敬请期待"
  },
  {
    title: "vue-router",
    content: "敬请期待"
  },
  {
    title: "mock",
    content: "敬请期待"
  },
  {
    title: "浏览器内核",
    content: "敬请期待"
  },
  {
    title: "performance",
    content: "敬请期待"
  },
  {
    title: "react.js",
    content: "敬请期待"
  },
  {
    title: "python",
    content: "敬请期待",

  },
  {
    title: "数据结构与算法",
    content: "敬请期待"
  },
  {
    title: "vuex",
    content: "敬请期待"
  },
  {
    title: "webpack",
    content: "敬请期待"
  },
  {
    title: "随笔",
    content: "敬请期待"
  },
  {
    title: "mongodb",
    content: "敬请期待"
  },
  {
    title: "express",
    content: "敬请期待"
  },
  {
    title: "关于博主",
    content: "敬请期待"
  },
  {
    title: "node",
    content: "敬请期待"
  },
  {
    title: "javaScript",
    content: "敬请期待"
  }
];
Mock.mock("/login", "post", ops => {
  ops = JSON.parse(ops.body);
  return loginByUsername(ops);
});
Mock.mock("/register", "post", ops => {
  ops = JSON.parse(ops.body);
  return registerUser(ops);
});
const loginByUsername = function(ops) {
  let meta = {
    msg: "登录成功",
    status: 200
  };
  let meta2 = {
    msg: "登录失败",
    status: 400
  };
  const result = userList.some(
    item => item.username === ops.username && item.password === ops.password
  );
  if (result) return meta;
  return meta2;
};
const registerUser = function(ops) {
  let meta = {
    msg: "注册成功",
    status: 200
  };
  let meta2 = {
    msg: "注册失败,( ′◔ ‸◔`)有此账户",
    status: 400
  };
  const result = userList.some(
    item => item.username === ops.username || item.mobile === ops.mobile
  );
  if (!result) {
    userList.push(ops);
    return meta;
  }
  return meta2;
};
const getTagList = function() {
  return tagList;
};
const getArticles = function() {
  let articlesList = [];
  for (let i = 0; i < 100; i++) {
    let article = {
      title: Randon.ctitle(),
      content: Randon.cparagraph(500),
      createdTime: Randon.date()
    };
    articlesList.push(article);
  }
  return articlesList;
};
Mock.mock("/tags", getTagList);
Mock.mock("/articles", getArticles);
